﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UnionLotto
{
    public partial class Form1 : Form
    {
        private static List<int> array = new List<int>(6);
        private Action<Control, int> func;
        private static readonly object _lock = new object();
        private Random rd = new Random();
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private List<Task> tasks = new List<Task>();
        private static List<BallModelEventArgs> args = new List<BallModelEventArgs>();
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 点击开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //分为两部分，红球和蓝球，蓝球1-16随机数据，红球1-33随机不重复
            func = new Action<Control, int>(BlueBall);
            //定时器每隔5秒检查一次是否重复
            timer.Enabled = true;
            timer.Interval = 5000;
            timer.Tick += ChangeSign;
            timer.Start();
            TaskFactory taskFactory = new TaskFactory();
            foreach (Control control in this.Controls)
            {
                if (control is Label)
                {
                    var obj = new BallModelEventArgs() { result = false, control = control };
                    args.Add(obj);
                    if ((control as Label).Text == "label7")
                    {
                        //蓝色球执行逻辑
                        tasks.Add(taskFactory.StartNew(() =>
                        {
                            BlueBallRunLogic(obj);
                        }));
                    }
                    else
                    {
                        //红色球执行逻辑
                        tasks.Add(taskFactory.StartNew(() =>
                        {
                            RedBallRunLogic(obj);
                        }));
                    }
                }
            }
            Task.WhenAll(tasks).ContinueWith((t) =>
            {
                timer.Dispose();
                MessageBox.Show("执行完成");
            });
        }

        /// <summary>
        /// 球赋值
        /// </summary>
        private void BlueBall(Control control, int state)
        {
                control.Text = state.ToString();
        }
        /// <summary>
        /// 判断值是否重复
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        private bool CheckValue(int state)
        {
            lock (_lock)
            {
                return array.Any(x => x == state);
            }
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="state"></param>
        private void InsertValue(int state)
        {
            lock (_lock)
            {
                array.Add(state);
            }
        }
        /// <summary>
        /// 红色球开始执行逻辑
        /// </summary>
        /// <param name="control"></param>
        /// <param name="state"></param>
        private void RedBallRunLogic(BallModelEventArgs args)
        {
            do
            {
                int value = rd.Next(1, 34);
                args.number = value;
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(func, args.control, value);
                }
                Thread.Sleep(50);
            } while (!args.result);
        }
        /// <summary>
        /// 蓝色球开始执行逻辑
        /// </summary>
        /// <param name="control"></param>
        /// <param name="state"></param>
        private void BlueBallRunLogic(BallModelEventArgs args)
        {
            do
            {
                int value = rd.Next(1, 17);
                args.number = value;
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(func, args.control, value);
                }
                Thread.Sleep(50);
            } while (!args.result);
        }
        /// <summary>
        /// 修改定时状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeSign(object sender, EventArgs e)
        {
            //1.先获取未修改的参数集合
            var obj = args.Where(x => x.result == false).FirstOrDefault();
            if (obj!=null && !obj.result)
            {
                if(obj.control.Text == "label7") {
                    //停止蓝色球
                    obj.result = true;
                } else
                {
                    //停止红色球先判断是否重复
                    if (!CheckValue(obj.number))
                    {
                        InsertValue(obj.number);
                        obj.result = true;
                    }
                }
            }
        }
    }
}
